<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>排序算法 | And forever has no end.</title><meta name="keywords" content="JAVA,算法,排序"><meta name="author" content="Zero"><meta name="copyright" content="Zero"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="AbsSort 排序抽象类1234567891011121314151617181920212223package com.java8.sort;&#x2F;** * 抽象类Sort * @author zero *&#x2F;public abstract class AbsSort &amp;#123;    public static void disPlay(int[] array) &amp;#123;        fo">
<meta property="og:type" content="article">
<meta property="og:title" content="排序算法">
<meta property="og:url" content="http://yamtg.gitee.io/2019/03/20/2019-03-20/index.html">
<meta property="og:site_name" content="And forever has no end.">
<meta property="og:description" content="AbsSort 排序抽象类1234567891011121314151617181920212223package com.java8.sort;&#x2F;** * 抽象类Sort * @author zero *&#x2F;public abstract class AbsSort &amp;#123;    public static void disPlay(int[] array) &amp;#123;        fo">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://yamtg.gitee.io/img/q6.png">
<meta property="article:published_time" content="2019-03-20T14:02:35.000Z">
<meta property="article:modified_time" content="2020-01-06T01:00:56.562Z">
<meta property="article:author" content="Zero">
<meta property="article:tag" content="JAVA">
<meta property="article:tag" content="算法">
<meta property="article:tag" content="排序">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://yamtg.gitee.io/img/q6.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://yamtg.gitee.io/2019/03/20/2019-03-20/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-01-06 09:00:56'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/img/q2.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">14</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">25</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">4</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> List</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/music/"><i class="fa-fw fas fa-music"></i><span> Music</span></a></li><li><a class="site-page" href="/movies/"><i class="fa-fw fas fa-video"></i><span> Movie</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/q6.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">And forever has no end.</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> List</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/music/"><i class="fa-fw fas fa-music"></i><span> Music</span></a></li><li><a class="site-page" href="/movies/"><i class="fa-fw fas fa-video"></i><span> Movie</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">排序算法</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-03-20T14:02:35.000Z" title="发表于 2019-03-20 22:02:35">2019-03-20</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2020-01-06T01:00:56.562Z" title="更新于 2020-01-06 09:00:56">2020-01-06</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/JAVA/">JAVA</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.3k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>12分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="AbsSort-排序抽象类"><a href="#AbsSort-排序抽象类" class="headerlink" title="AbsSort 排序抽象类"></a>AbsSort 排序抽象类</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.java8.sort;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 抽象类Sort</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zero</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">AbsSort</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">disPlay</span><span class="params">(<span class="keyword">int</span>[] array)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i : array) &#123;</span><br><span class="line">            System.out.print(i + <span class="string">&quot; &quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">swap</span><span class="params">(<span class="keyword">int</span>[] array, <span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> temp = array[x];</span><br><span class="line">        array[x] = array[y];</span><br><span class="line">        array[y] = temp;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<span id="more"></span>

<h2 id="BubbleSort-冒泡排序"><a href="#BubbleSort-冒泡排序" class="headerlink" title="BubbleSort 冒泡排序"></a>BubbleSort 冒泡排序</h2><p><code>算法思想：每一次排序都会将较大值交换到数组末尾</code><br><code>算法平均复杂度：O(n^2)</code><br><img src="http://image.ifamily.wang/2019-03-23-0B1FD7EB-6749-4D85-8B15-D6C926BC3AC7.gif" alt="0B1FD7EB-6749-4D85-8B15-D6C926BC3AC7"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.java8.sort;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 冒泡排序</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zero</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BubbleSort</span> <span class="keyword">extends</span> <span class="title">AbsSort</span> </span>&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span>[] array = &#123;<span class="number">12</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">5</span>, <span class="number">12</span>, <span class="number">5</span>, <span class="number">17</span>, <span class="number">91</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">22</span>, <span class="number">8</span>&#125;;</span><br><span class="line"></span><br><span class="line">        sort(array);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> array</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sort</span><span class="params">(<span class="keyword">int</span>[] array)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; array.length; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; array.length - i - <span class="number">1</span>; j++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (array[j] &gt; array[j + <span class="number">1</span>]) &#123;</span><br><span class="line">                    swap(array, j, j + <span class="number">1</span>);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            disPlay(array);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">  </span><br></pre></td></tr></table></figure>

<p>每趟排序结果：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">3 1 5 12 5 12 17 3 2 4 22 8 91 </span><br><span class="line">1 3 5 5 12 12 3 2 4 17 8 22 91 </span><br><span class="line">1 3 5 5 12 3 2 4 12 8 17 22 91 </span><br><span class="line">1 3 5 5 3 2 4 12 8 12 17 22 91 </span><br><span class="line">1 3 5 3 2 4 5 8 12 12 17 22 91 </span><br><span class="line">1 3 3 2 4 5 5 8 12 12 17 22 91 </span><br><span class="line">1 3 2 3 4 5 5 8 12 12 17 22 91 </span><br><span class="line">1 2 3 3 4 5 5 8 12 12 17 22 91 </span><br><span class="line">1 2 3 3 4 5 5 8 12 12 17 22 91 </span><br><span class="line">1 2 3 3 4 5 5 8 12 12 17 22 91 </span><br><span class="line">1 2 3 3 4 5 5 8 12 12 17 22 91 </span><br><span class="line">1 2 3 3 4 5 5 8 12 12 17 22 91 </span><br><span class="line">1 2 3 3 4 5 5 8 12 12 17 22 91 </span><br></pre></td></tr></table></figure>

<h2 id="InsertSort-插入排序"><a href="#InsertSort-插入排序" class="headerlink" title="InsertSort 插入排序"></a>InsertSort 插入排序</h2><p><code>算法思想：每一次排序都把最后一位和前面已排序好的队列比较，直到找到插入的位置插入</code><br><code>算法平均复杂度：O(n^2)</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.java8.sort;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 插入排序</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zero</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">InsertSort</span> <span class="keyword">extends</span> <span class="title">AbsSort</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span>[] array = &#123;<span class="number">12</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">5</span>, <span class="number">12</span>, <span class="number">5</span>, <span class="number">17</span>, <span class="number">91</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">22</span>, <span class="number">8</span>&#125;;</span><br><span class="line">        sort(array);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sort</span><span class="params">(<span class="keyword">int</span>[] array)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">final</span> <span class="keyword">int</span> length = array.length;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (array[i - <span class="number">1</span>] &gt; array[i]) &#123;</span><br><span class="line">                <span class="keyword">int</span> x = array[i];</span><br><span class="line">                <span class="keyword">int</span> j = i - <span class="number">1</span>;</span><br><span class="line">                array[i] = array[j];</span><br><span class="line"></span><br><span class="line">                <span class="comment">// 找到j位置比 x 小的值</span></span><br><span class="line">                <span class="keyword">while</span> (j &gt;= <span class="number">0</span> &amp;&amp; x &lt; array[j]) &#123;</span><br><span class="line">                    array[j + <span class="number">1</span>] = array[j];</span><br><span class="line">                    j--;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">// 在 j + 1 位置插入</span></span><br><span class="line">                array[j + <span class="number">1</span>] = x;</span><br><span class="line">            &#125;</span><br><span class="line">            disPlay(array);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br></pre></td></tr></table></figure>

<p>每趟排序结果：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">3 12 1 5 12 5 17 91 3 2 4 22 8 </span><br><span class="line">1 3 12 5 12 5 17 91 3 2 4 22 8 </span><br><span class="line">1 3 5 12 12 5 17 91 3 2 4 22 8 </span><br><span class="line">1 3 5 12 12 5 17 91 3 2 4 22 8 </span><br><span class="line">1 3 5 5 12 12 17 91 3 2 4 22 8 </span><br><span class="line">1 3 5 5 12 12 17 91 3 2 4 22 8 </span><br><span class="line">1 3 5 5 12 12 17 91 3 2 4 22 8 </span><br><span class="line">1 3 3 5 5 12 12 17 91 2 4 22 8 </span><br><span class="line">1 2 3 3 5 5 12 12 17 91 4 22 8 </span><br><span class="line">1 2 3 3 4 5 5 12 12 17 91 22 8 </span><br><span class="line">1 2 3 3 4 5 5 12 12 17 22 91 8 </span><br><span class="line">1 2 3 3 4 5 5 8 12 12 17 22 91 </span><br></pre></td></tr></table></figure>

<h2 id="QuickSort-快速排序"><a href="#QuickSort-快速排序" class="headerlink" title="QuickSort 快速排序"></a>QuickSort 快速排序</h2><p><code>算法思想：寻找当前元素合适的位置，左边集合比当前元素小，右边集合比当前元素大。分区（分而治之）完成后继续开始寻找当前元素合适的位置</code><br><code>算法平均复杂度：O(nlgn)</code><br><img src="http://image.ifamily.wang/2019-03-23-32567AE4-F28B-40C0-B17B-37E9EACFB1E8.gif" alt="32567AE4-F28B-40C0-B17B-37E9EACFB1E8"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.java8.sort;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 快速排序</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zero</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">QuickSort</span>  <span class="keyword">extends</span> <span class="title">AbsSort</span></span>&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span>[] array = &#123;<span class="number">12</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">5</span>, <span class="number">12</span>, <span class="number">5</span>, <span class="number">17</span>, <span class="number">91</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">22</span>, <span class="number">8</span>&#125;;</span><br><span class="line">        disPlay(array);</span><br><span class="line">        sort(array);</span><br><span class="line">        disPlay(array);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sort</span><span class="params">(<span class="keyword">int</span>[] array)</span> </span>&#123;</span><br><span class="line">        sort(array, <span class="number">0</span>, array.length - <span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sort</span><span class="params">(<span class="keyword">int</span>[] array, <span class="keyword">int</span> low, <span class="keyword">int</span> hight)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(low &lt; hight)&#123;</span><br><span class="line">            <span class="keyword">int</span> p = partition(array, low, hight);</span><br><span class="line">            sort(array, low, p - <span class="number">1</span>);</span><br><span class="line">            sort(array, p + <span class="number">1</span>, hight);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">partition</span><span class="params">(<span class="keyword">int</span>[] array, <span class="keyword">int</span> low, <span class="keyword">int</span> hight)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 选择第一位开始分区</span></span><br><span class="line">        <span class="keyword">int</span> privotKey = array[low];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> (low &lt; hight) &#123;</span><br><span class="line">            <span class="comment">// 筛选右边比privotKey小的</span></span><br><span class="line">            <span class="keyword">while</span> (low &lt; hight &amp;&amp; array[hight] &gt;= privotKey) &#123;</span><br><span class="line">                hight--;</span><br><span class="line">            &#125;</span><br><span class="line">            swap(array, low, hight);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 筛选左边比privotKey大的</span></span><br><span class="line">            <span class="keyword">while</span> (low &lt; hight &amp;&amp; array[low] &lt;= privotKey) &#123;</span><br><span class="line">                low++;</span><br><span class="line">            &#125;</span><br><span class="line">            swap(array, low, hight);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> low;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>每趟排序结果：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">8 3 1 5 12 5 4 2 3 12 91 22 17 </span><br><span class="line">3 3 1 5 2 5 4 8 12 12 91 22 17 </span><br><span class="line">2 3 1 3 5 5 4 8 12 12 91 22 17 </span><br><span class="line">1 2 3 3 5 5 4 8 12 12 91 22 17 </span><br><span class="line">1 2 3 3 4 5 5 8 12 12 91 22 17 </span><br><span class="line">1 2 3 3 4 5 5 8 12 12 91 22 17 </span><br><span class="line">1 2 3 3 4 5 5 8 12 12 17 22 91 </span><br><span class="line">1 2 3 3 4 5 5 8 12 12 17 22 91 </span><br></pre></td></tr></table></figure>

<h2 id="SelectionSort-选择排序"><a href="#SelectionSort-选择排序" class="headerlink" title="SelectionSort 选择排序"></a>SelectionSort 选择排序</h2><p><code>算法思想：每一趟选择剩余元素中最小的元素放在已排序好的元素后面</code><br><code>算法平均复杂度：O(n^2)</code><br><img src="http://image.ifamily.wang/2019-03-23-53886634-0498-49FF-A904-7E10E531AE4B.gif" alt="53886634-0498-49FF-A904-7E10E531AE4B"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">package</span> com.java8.sort;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 选择排序</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zero</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SelectionSort</span> <span class="keyword">extends</span> <span class="title">AbsSort</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span>[] array = &#123;<span class="number">12</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">5</span>, <span class="number">12</span>, <span class="number">5</span>, <span class="number">17</span>, <span class="number">91</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">22</span>, <span class="number">8</span>&#125;;</span><br><span class="line">        disPlay(array);</span><br><span class="line">        sort(array);</span><br><span class="line">        disPlay(array);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sort</span><span class="params">(<span class="keyword">int</span>[] array)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; array.length; i++) &#123;</span><br><span class="line">            <span class="keyword">int</span> min = i;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = i; j &lt; array.length; j++) &#123;</span><br><span class="line">                <span class="comment">// 选择一个比min小的，并更新min</span></span><br><span class="line">                <span class="keyword">if</span> (array[min] &gt; array[j]) &#123;</span><br><span class="line">                    min = j;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            swap(array, i, min);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>每趟排序结果：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">1 3 12 5 12 5 17 91 3 2 4 22 8 </span><br><span class="line">1 2 12 5 12 5 17 91 3 3 4 22 8 </span><br><span class="line">1 2 3 5 12 5 17 91 12 3 4 22 8 </span><br><span class="line">1 2 3 3 12 5 17 91 12 5 4 22 8 </span><br><span class="line">1 2 3 3 4 5 17 91 12 5 12 22 8 </span><br><span class="line">1 2 3 3 4 5 17 91 12 5 12 22 8 </span><br><span class="line">1 2 3 3 4 5 5 91 12 17 12 22 8 </span><br><span class="line">1 2 3 3 4 5 5 8 12 17 12 22 91 </span><br><span class="line">1 2 3 3 4 5 5 8 12 17 12 22 91 </span><br><span class="line">1 2 3 3 4 5 5 8 12 12 17 22 91 </span><br><span class="line">1 2 3 3 4 5 5 8 12 12 17 22 91 </span><br><span class="line">1 2 3 3 4 5 5 8 12 12 17 22 91 </span><br><span class="line">1 2 3 3 4 5 5 8 12 12 17 22 91 </span><br></pre></td></tr></table></figure>


<h2 id="MaxHeapSort-最大堆排序"><a href="#MaxHeapSort-最大堆排序" class="headerlink" title="MaxHeapSort 最大堆排序"></a>MaxHeapSort 最大堆排序</h2><p><code>算法思想：构建最大堆后，每一次把最大堆的根节点和没交换过最后的叶子节点交换，重新构建最大堆</code><br><code>算法平均复杂度：O(nlgn)</code><br><img src="http://image.ifamily.wang/2019-03-23-85E24176-2625-4EB9-BD80-F32A1E82526D.gif" alt="85E24176-2625-4EB9-BD80-F32A1E82526D"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.java8.sort;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 最大堆排序</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zero</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MaxHeapSort</span> <span class="keyword">extends</span> <span class="title">AbsSort</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span>[] data;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> capacity;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> count;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">MaxHeapSort</span><span class="params">(<span class="keyword">int</span>[] array, <span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.data = <span class="keyword">new</span> <span class="keyword">int</span>[n + <span class="number">1</span>];</span><br><span class="line">        <span class="keyword">this</span>.capacity = n;</span><br><span class="line">        <span class="keyword">this</span>.count = n;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            data[i + <span class="number">1</span>] = array[i];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        buildHeap();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">buildHeap</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> parent = capacity / <span class="number">2</span>; parent &gt;= <span class="number">1</span>; parent--) &#123;</span><br><span class="line">            shiftDown(parent);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">shiftDown</span><span class="params">(<span class="keyword">int</span> parent)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> left = parent * <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">int</span> right = parent * <span class="number">2</span> + <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">int</span> maxValueIndex = left;</span><br><span class="line">        <span class="keyword">while</span> (left &lt;= capacity) &#123;</span><br><span class="line">            <span class="keyword">if</span> (right &lt;= capacity &amp;&amp; data[right] &gt; data[left]) &#123;</span><br><span class="line">                maxValueIndex = right;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (data[parent] &gt;= data[maxValueIndex]) &#123;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            swap(data, parent, maxValueIndex);</span><br><span class="line">            shiftDown(maxValueIndex);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sort</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; count; i++) &#123;</span><br><span class="line">            swap(data, <span class="number">1</span>, capacity--);</span><br><span class="line">            buildHeap();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">disPlay</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        disPlay(<span class="keyword">this</span>.data);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span>[] array = &#123;<span class="number">12</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">5</span>, <span class="number">12</span>, <span class="number">5</span>, <span class="number">17</span>, <span class="number">91</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">22</span>, <span class="number">8</span>&#125;;</span><br><span class="line">        MaxHeapSort maxHeapSort = <span class="keyword">new</span> MaxHeapSort(array, array.length);</span><br><span class="line">        maxHeapSort.disPlay();</span><br><span class="line">        maxHeapSort.sort();</span><br><span class="line">        maxHeapSort.disPlay();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>每趟排序结果：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">初始化最大堆</span><br><span class="line">0 91 12 22 5 12 8 17 3 3 2 4 5 1 </span><br><span class="line">开始排序</span><br><span class="line">0 22 12 17 5 12 8 1 3 3 2 4 5 91 </span><br><span class="line">0 17 12 8 5 12 5 1 3 3 2 4 22 91 </span><br><span class="line">0 12 12 8 5 4 5 1 3 3 2 17 22 91 </span><br><span class="line">0 12 5 8 3 4 5 1 2 3 12 17 22 91 </span><br><span class="line">0 8 5 5 3 4 3 1 2 12 12 17 22 91 </span><br><span class="line">0 5 4 5 3 2 3 1 8 12 12 17 22 91 </span><br><span class="line">0 5 4 3 3 2 1 5 8 12 12 17 22 91 </span><br><span class="line">0 4 3 3 1 2 5 5 8 12 12 17 22 91 </span><br><span class="line">0 3 2 3 1 4 5 5 8 12 12 17 22 91 </span><br><span class="line">0 3 2 1 3 4 5 5 8 12 12 17 22 91 </span><br><span class="line">0 2 1 3 3 4 5 5 8 12 12 17 22 91 </span><br><span class="line">0 1 2 3 3 4 5 5 8 12 12 17 22 91 </span><br></pre></td></tr></table></figure>

<h2 id="MergeSort-归并排序"><a href="#MergeSort-归并排序" class="headerlink" title="MergeSort 归并排序"></a>MergeSort 归并排序</h2><p><code>算法思想：该算法采用分而治之的方法，拆分后排序合并</code><br><code>算法平均复杂度：O(nlgn)</code></p>
<p><img src="http://image.ifamily.wang/2019-03-23-C886DA46-2F51-416A-9D7F-E383EFAFF272.gif" alt="C886DA46-2F51-416A-9D7F-E383EFAFF272"></p>
<p><img src="http://image.ifamily.wang/2019-03-23-15533528633621.png"></p>
<p>合并过程<br><img src="http://image.ifamily.wang/2019-03-23-15533528776232.png"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.java8.sort;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 合并排序</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zero</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MergeSort</span> <span class="keyword">extends</span> <span class="title">AbsSort</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span>[] array = &#123;<span class="number">12</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">5</span>, <span class="number">12</span>, <span class="number">5</span>, <span class="number">17</span>, <span class="number">91</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">22</span>, <span class="number">8</span>&#125;;</span><br><span class="line">        sort(array);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sort</span><span class="params">(<span class="keyword">int</span>[] array)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span>[] temp = <span class="keyword">new</span> <span class="keyword">int</span>[array.length];</span><br><span class="line">        sort(array, <span class="number">0</span>, array.length - <span class="number">1</span>, temp);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sort</span><span class="params">(<span class="keyword">int</span>[] array, <span class="keyword">int</span> left, <span class="keyword">int</span> right, <span class="keyword">int</span>[] temp)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (left &lt; right) &#123;</span><br><span class="line">            <span class="keyword">int</span> mid = (left + right) / <span class="number">2</span>;</span><br><span class="line">            <span class="comment">// 分</span></span><br><span class="line">            sort(array, left, mid, temp);</span><br><span class="line">            sort(array, mid + <span class="number">1</span>, right, temp);</span><br><span class="line">            <span class="comment">// 治</span></span><br><span class="line">            merge(array, left, mid, right, temp);</span><br><span class="line">            disPlay(array);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">merge</span><span class="params">(<span class="keyword">int</span>[] array, <span class="keyword">int</span> left, <span class="keyword">int</span> mid, <span class="keyword">int</span> right, <span class="keyword">int</span>[] temp)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//左序列指针</span></span><br><span class="line">        <span class="keyword">int</span> i = left;</span><br><span class="line">        <span class="comment">//右序列指针</span></span><br><span class="line">        <span class="keyword">int</span> j = mid + <span class="number">1</span>;</span><br><span class="line">        <span class="comment">//临时数组指针</span></span><br><span class="line">        <span class="keyword">int</span> t = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> (i &lt;= mid &amp;&amp; j &lt;= right) &#123;</span><br><span class="line">            <span class="keyword">if</span> (array[i] &lt;= array[j]) &#123;</span><br><span class="line">                temp[t++] = array[i++];</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                temp[t++] = array[j++];</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//将左边剩余元素填充进temp中</span></span><br><span class="line">        <span class="keyword">while</span> (i &lt;= mid) &#123;</span><br><span class="line">            temp[t++] = array[i++];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//将右序列剩余元素填充进temp中</span></span><br><span class="line">        <span class="keyword">while</span> (j &lt;= right) &#123;</span><br><span class="line">            temp[t++] = array[j++];</span><br><span class="line">        &#125;</span><br><span class="line">        t = <span class="number">0</span>;</span><br><span class="line">        <span class="comment">//将temp中的元素全部拷贝到原数组中</span></span><br><span class="line">        <span class="keyword">while</span> (left &lt;= right) &#123;</span><br><span class="line">            array[left++] = temp[t++];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>每趟排序结果：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">3 12 1 5 12 5 17 91 3 2 4 22 8 </span><br><span class="line">3 12 1 5 12 5 17 91 3 2 4 22 8 </span><br><span class="line">1 3 5 12 12 5 17 91 3 2 4 22 8 </span><br><span class="line">1 3 5 12 5 12 17 91 3 2 4 22 8 </span><br><span class="line">1 3 5 12 5 12 17 91 3 2 4 22 8 </span><br><span class="line">1 3 5 5 12 12 17 91 3 2 4 22 8 </span><br><span class="line">1 3 5 5 12 12 17 3 91 2 4 22 8 </span><br><span class="line">1 3 5 5 12 12 17 2 3 91 4 22 8 </span><br><span class="line">1 3 5 5 12 12 17 2 3 91 4 22 8 </span><br><span class="line">1 3 5 5 12 12 17 2 3 91 4 8 22 </span><br><span class="line">1 3 5 5 12 12 17 2 3 4 8 22 91 </span><br><span class="line">1 2 3 3 4 5 5 8 12 12 17 22 91 </span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Zero</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://yamtg.gitee.io/2019/03/20/2019-03-20/">http://yamtg.gitee.io/2019/03/20/2019-03-20/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://yamtg.gitee.io" target="_blank">And forever has no end.</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/JAVA/">JAVA</a><a class="post-meta__tags" href="/tags/%E7%AE%97%E6%B3%95/">算法</a><a class="post-meta__tags" href="/tags/%E6%8E%92%E5%BA%8F/">排序</a></div><div class="post_share"><div class="social-share" data-image="/img/q6.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2019/03/23/2019-03-23/"><img class="prev-cover" src="/img/q6.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">最大堆</div></div></a></div><div class="next-post pull-right"><a href="/2017/12/25/2017-12-25/"><img class="next-cover" src="/img/q5.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">随笔(2017年12月25日10:00:31)</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2019/03/23/2019-03-23/" title="最大堆"><img class="cover" src="/img/q6.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-23</div><div class="title">最大堆</div></div></a></div><div><a href="/2020/01/06/2018-03-16/" title="Spring Cloud 标准架构图"><img class="cover" src="/img/q5.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-01-06</div><div class="title">Spring Cloud 标准架构图</div></div></a></div><div><a href="/2019/03/28/2019-03-28-1/" title="AbstractQueuedSynchronizer"><img class="cover" src="/img/q4.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-28</div><div class="title">AbstractQueuedSynchronizer</div></div></a></div><div><a href="/2019/03/28/2019-03-28/" title="TwinsLock 自定义锁"><img class="cover" src="/img/q3.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-28</div><div class="title">TwinsLock 自定义锁</div></div></a></div><div><a href="/2019/03/29/2019-03-29/" title="HashMap 散列hash算法"><img class="cover" src="/img/q1.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-29</div><div class="title">HashMap 散列hash算法</div></div></a></div><div><a href="/2020/01/06/2020-01-06/" title="Web Filter 过滤器"><img class="cover" src="/img/q1.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-01-06</div><div class="title">Web Filter 过滤器</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="/img/q2.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">Zero</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">14</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">25</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">4</div></a></div></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#AbsSort-%E6%8E%92%E5%BA%8F%E6%8A%BD%E8%B1%A1%E7%B1%BB"><span class="toc-number">1.</span> <span class="toc-text">AbsSort 排序抽象类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#BubbleSort-%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F"><span class="toc-number">2.</span> <span class="toc-text">BubbleSort 冒泡排序</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#InsertSort-%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F"><span class="toc-number">3.</span> <span class="toc-text">InsertSort 插入排序</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#QuickSort-%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F"><span class="toc-number">4.</span> <span class="toc-text">QuickSort 快速排序</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#SelectionSort-%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA%8F"><span class="toc-number">5.</span> <span class="toc-text">SelectionSort 选择排序</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#MaxHeapSort-%E6%9C%80%E5%A4%A7%E5%A0%86%E6%8E%92%E5%BA%8F"><span class="toc-number">6.</span> <span class="toc-text">MaxHeapSort 最大堆排序</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#MergeSort-%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F"><span class="toc-number">7.</span> <span class="toc-text">MergeSort 归并排序</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2020/01/06/2020-01-06/" title="Web Filter 过滤器"><img src="/img/q1.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Web Filter 过滤器"/></a><div class="content"><a class="title" href="/2020/01/06/2020-01-06/" title="Web Filter 过滤器">Web Filter 过滤器</a><time datetime="2020-01-06T01:03:36.000Z" title="发表于 2020-01-06 09:03:36">2020-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2020/01/06/2018-03-16/" title="Spring Cloud 标准架构图"><img src="/img/q5.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Spring Cloud 标准架构图"/></a><div class="content"><a class="title" href="/2020/01/06/2018-03-16/" title="Spring Cloud 标准架构图">Spring Cloud 标准架构图</a><time datetime="2020-01-06T01:00:56.561Z" title="发表于 2020-01-06 09:00:56">2020-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2020/01/06/2018-06-20/" title="北京最新地铁坐标（高德地图）"><img src="/img/q1.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="北京最新地铁坐标（高德地图）"/></a><div class="content"><a class="title" href="/2020/01/06/2018-06-20/" title="北京最新地铁坐标（高德地图）">北京最新地铁坐标（高德地图）</a><time datetime="2020-01-06T01:00:56.561Z" title="发表于 2020-01-06 09:00:56">2020-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2019/04/09/2019-04-09/" title="设计模式"><img src="/img/q5.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="设计模式"/></a><div class="content"><a class="title" href="/2019/04/09/2019-04-09/" title="设计模式">设计模式</a><time datetime="2019-04-09T05:56:26.000Z" title="发表于 2019-04-09 13:56:26">2019-04-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2019/04/03/2019-04-03/" title="Zookeeper 选举"><img src="/img/q6.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Zookeeper 选举"/></a><div class="content"><a class="title" href="/2019/04/03/2019-04-03/" title="Zookeeper 选举">Zookeeper 选举</a><time datetime="2019-04-03T01:49:52.000Z" title="发表于 2019-04-03 09:49:52">2019-04-03</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By Zero</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="true" data-click="true"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>